package com.situ.feemanagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.feemanagement.listener.MedicineExcelListener;
import com.situ.feemanagement.mapper.MedicineMapper;
import com.situ.feemanagement.pojo.Medicine;
import com.situ.feemanagement.pojo.query.MedicineQuery;
import com.situ.feemanagement.pojo.vo.InventoryVO;
import com.situ.feemanagement.pojo.vo.MedicineVO;
import com.situ.feemanagement.service.IMedicineService;
import com.situ.feemanagement.util.ExcelUtils;
import com.situ.feemanagement.util.PageResult;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MedicineServiceImpl implements IMedicineService {

    @Autowired
    private MedicineMapper medicineMapper;

    @Override
    public List<Medicine> selectAll() {
        return medicineMapper.selectAll();
    }

    @Override
    public PageResult<MedicineVO> selectByPage(MedicineQuery medicineQuery) {
        PageHelper.startPage(medicineQuery.getPage(), medicineQuery.getLimit());
        List<MedicineVO> list = medicineMapper.selectByPage(medicineQuery);
        PageInfo<MedicineVO> pageInfo = new PageInfo<>(list);
        int totalCount = (int) pageInfo.getTotal();
        return new PageResult<>(0, "", totalCount, list);
    }

    @Override
    public void deleteById(int id) {
        medicineMapper.deleteById(id);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            medicineMapper.deleteById(Integer.parseInt(id));
        }
    }

    @Override
    public void add(Medicine medicine) {
        medicineMapper.add(medicine);
    }

    @Override
    public Medicine selectById(int id) {
        return medicineMapper.selectById(id);
    }

    @Override
    public void update(Medicine medicine) {
        medicineMapper.update(medicine);
    }

    @Override
    public List<InventoryVO> selectQuantity(int id) {
        List<InventoryVO> list = new ArrayList<>();
        Medicine medicine = medicineMapper.selectById(id);
        for (int i = 1; i <= medicine.getInventory(); i++) {
            InventoryVO quantity = new InventoryVO();
            quantity.setQuantity(i);
            list.add(quantity);
        }
        return list;
    }

    @Override
    public void importExcel(HttpServletRequest request) {
        //ExcelUtils.importExcel(request,new MedicineExcelListener(),Medicine.class);
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        List<Medicine> list = medicineMapper.selectAll();
        ExcelUtils.exportExcel(response, list, Medicine.class, "medicine");
    }
}
